package com.hongkuncheng.cms.controller.admin;

import java.io.File;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.FileHelper;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.Datamodel;
import com.hongkuncheng.cms.model.Department;
import com.hongkuncheng.cms.model.Webinfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 部门管理
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/department")
public class AdepartmentController extends BaseController {

    // 部门列表
    @RequestMapping
    public String index() {
        List<Department> departments = factorySession.createCriteria(Department.class)
                .addOrder(Order.asc("sort")).list();
        request.setAttribute("departments", departments);
        return view();
    }

    // 部门编辑
    @RequestMapping("edit")
    public String edit() {
        int departmentId = request.getParameter("departmentid") == null ? 0 :
                Integer.parseInt(request.getParameter("departmentid"));
        Department department = departmentId == 0 ? new Department() : factorySession.get(Department.class, departmentId);
        if (departmentId == 0) {
            Object uniqueResult = factorySession.createCriteria(Department.class)
                    .setProjection(Projections.max("sort")).uniqueResult();
            int maxSort = uniqueResult == null ? 0 : Integer.parseInt(uniqueResult.toString());
            department.setSort(++maxSort);
        }
        request.setAttribute("department", department);
        List<Datamodel> datamodels = factorySession.createCriteria(Datamodel.class)
                .add(Restrictions.eq("datamodelType.id", 4)).list();
        request.setAttribute("datamodels", datamodels);
        webinfo = factorySession.get(Webinfo.class, 1);
        List<File> templatefiles = FileHelper.getFiles(applicationDirectory + "/template/" + webinfo.getTemplate());
        request.setAttribute("templatefiles", templatefiles);
        return view();
    }

    // 部门编辑处理
    @RequestMapping("editdo")
    @ResponseBody
    public void editdo() {
        checkAdminPermission("yunwei_organ_department");
        int departmentId = Integer.parseInt(request.getParameter("departmentid"));
        String name = request.getParameter("name");
        int exist = Integer.parseInt(factorySession.createCriteria(Department.class)
                .add(Restrictions.eq("name", name)).add(Restrictions.ne("id", departmentId))
                .setProjection(Projections.count("id")).uniqueResult().toString());
        if (exist > 0) {
            PageHelper.alertAndGoBack("部门名称已存在，请重新输入");
            return;
        }
        String urldirectory = request.getParameter("urldirectory");
        if (urldirectory != "") {
            exist = Integer.parseInt(factorySession.createCriteria(Department.class)
                    .add(Restrictions.eq("urldirectory", urldirectory))
                    .add(Restrictions.ne("id", departmentId))
                    .setProjection(Projections.count("id"))
                    .uniqueResult().toString());
            if (exist > 0) {
                PageHelper.alertAndGoBack("URL目录已存在，请重新输入");
                return;
            }
        }
        Department department = departmentId == 0 ? new Department() :
                factorySession.load(Department.class, departmentId, LockMode.PESSIMISTIC_WRITE);
        department.setName(name);
        department.setUrldirectory(urldirectory);
        int datamodelId = Integer.parseInt(request.getParameter("datamodelid"));
        if (datamodelId == 0) {
            department.setDatamodel(null);
        } else {
            Datamodel datamodel = factorySession.load(Datamodel.class, datamodelId);
            department.setDatamodel(datamodel);
        }
        String template = request.getParameter("template");
        if (template == "") department.setTemplate(null);
        else department.setTemplate(template);
        department.setSort(Integer.parseInt(request.getParameter("sort")));
        if (departmentId == 0) factorySession.save(department);
        recordLog((departmentId == 0 ? "新建" : "更新") + "部门《" + department.getName().trim() + "》");
        PageHelper.alertAndRedirect("保存成功", "./");
    }

    // 部门删除
    @RequestMapping("delete")
    @ResponseBody
    public void delete() {
        checkAdminPermission("yunwei_organ_department");
        int departmentId = Integer.parseInt(request.getParameter("departmentid"));
        Department department = factorySession.load(Department.class, departmentId, LockMode.PESSIMISTIC_WRITE);
        factorySession.delete(department);
        recordLog("删除部门《" + department.getName().trim() + "》");
        PageHelper.alertAndRedirect("删除成功", "./");
    }

}
